/**
 *  IntegerItem.java : IceBird project
 *  15:59:59 
 */
package icebird.compiler.x86.old;

import icebird.metadata.BasicType;
import icebird.compiler.x86.old.X86Register.GPR;

/**
 * @author Sergey Shulepoff[Knott]
 */
public final class IntegerItem extends WordItem {
	
	private int value;
	
	public IntegerItem(ItemFactory factory) {
		super(factory);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.x86.Item#getType()
	 */
	public BasicType getType() {
		return BasicType.Int; 
	}
	
	public final void init(EmitterContext c, byte k, short bp, X86Register reg, int v) {
		super.init(c, k, reg, bp);
		value = v;
	}
	
	/**
	 * Gets value.
	 * @return int
	 */
	public final int getValue() {
		return value;
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.x86.WordItem#loadToConstant(icebird.compiler.x86.EmitterContext, icebird.compiler.x86.X86Assembler, icebird.compiler.x86.X86Register.GPR)
	 */
	@Override
	protected void loadToConstant(EmitterContext ec, X86Assembler os, GPR reg) {
		if (value == 0) {
			os.clear(reg);
		} else if (value == 1) {
			os.clear(reg);
			os.inc(reg);
		} else if (value == -1) {
			os.clear(reg);
			os.dec(reg);
		} else {
			os.moveConst(reg, value);
		}
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.x86.WordItem#popFromFPU(icebird.compiler.x86.X86Assembler, icebird.compiler.x86.X86Register.GPR, int)
	 */
	@Override
	protected void popFromFPU(X86Assembler os, GPR reg, int disp) {
		os.fistp(reg, disp);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.x86.WordItem#pushToFPU(icebird.compiler.x86.X86Assembler, icebird.compiler.x86.X86Register.GPR, int)
	 */
	@Override
	protected void pushToFPU(X86Assembler os, GPR reg, int disp) {
		os.fild(reg, disp);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.x86.WordItem#cloneConstant(icebird.compiler.x86.EmitterContext)
	 */
	@Override
	protected WordItem cloneConstant(EmitterContext ec) {
		return creator.createIConst(ec, value);
	}

	/* (non-Javadoc)
	 * @see icebird.compiler.x86.WordItem#pushConstant(icebird.compiler.x86.EmitterContext, icebird.compiler.x86.X86Assembler)
	 */
	@Override
	protected void pushConstant(EmitterContext ec, X86Assembler os) {
		os.push(value);
	}
}
